package com.wannengbang.storemobile.homepage.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.wannengbang.storemobile.R;
import com.wannengbang.storemobile.bean.PurchaseDetailsBean;
import com.wannengbang.storemobile.utils.NumberFormatUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 */
public class PurchaseGoodsListAdapter extends RecyclerView.Adapter<PurchaseGoodsListAdapter.ViewHolder> {

    private OnCallBackListener onCallBackListener;
    private List<PurchaseDetailsBean.DataBean.ListBean> listsBeans;
    private Context context;

    public PurchaseGoodsListAdapter(List<PurchaseDetailsBean.DataBean.ListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_purchase_goods_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PurchaseDetailsBean.DataBean.ListBean dataBean = listsBeans.get(position);
        holder.tvGoodsName.setText(dataBean.getGoods_name());
        String goodsFee = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getGoods_fee()) / 100);
        holder.tvPrice.setText(goodsFee + "元");
        holder.tvGoodsNum.setText(dataBean.getGoods_count() + "");
        String totalPrice = NumberFormatUtils.formatDouble(Double.parseDouble(dataBean.getGoods_total_fee()) / 100);
        holder.tvTotalMoney.setText(totalPrice + "元");
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R.id.tv_price)
        TextView tvPrice;
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R.id.tv_total_money)
        TextView tvTotalMoney;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
